package com.nave.segundaguerra.servidorecliente.cliente;


import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.PointF;
import android.util.Log;
import android.widget.RelativeLayout;

import com.nave.segundaguerra.game.CalculoImagem;
import com.nave.segundaguerra.game.SpritePlayer;
import com.nave.segundaguerra.servidorecliente.util.Const;
import com.nave.segundaguerra.servidorecliente.util.Protocolo;


public class PlayerCliente 
{
	// Dados individuais do jogador
	
	private String nome;
	private String time;
	private String minhaClasse;
	
	private PointF position = new PointF();
	private PointF destination;

	private int balas = 20;
	private int municao = 7;
	private int pontos;
	private int vida;

	// Recursos
	private Bitmap imagemPlayer;
	private Bitmap tiro;
	private Point destinationTiro = new Point();
	protected int plusScrollX,plusScrollY;
	private Paint paint;
	private boolean telaCheia = false;
	private boolean drawTarget;
	private boolean rotateImage = false;
	private boolean isAtirou = false;
	private boolean rotateImageTiro;
	
	// Server
	public DadosDoCliente clientData;
	
	
	public PlayerCliente(){
		
	}
	public PlayerCliente(String nome, PointF position, String minhaClasse)
	{
		
		paint = new Paint();
		paint.setColor(Color.BLACK);
		paint.setTextSize(20);
		paint.setAntiAlias(true);
		paint.setDither(true);
		paint.setFilterBitmap(true);
		
		this.minhaClasse = minhaClasse;
		this.nome = nome;
		this.position = position;
		
		if(this.minhaClasse != "General"){
		}
		atualizarPaint();
	}
	
	public void draw(Canvas canvas)
	{
		if(this.imagemPlayer != null){
			canvas.drawBitmap(this.imagemPlayer,this.position.x, this.position.y, paint);
		}else{
		canvas.drawCircle(this.position.x, this.position.y, 10, new Paint());
		}
	}
	public double getAngle(double x1, double y1, double x2, double y2)
	{
		return Math.toDegrees(Math.atan2(x2-x1,y1-y2));
	}
	public double getAngleFire(double x1, double y1, double x2, double y2)
	{
		//return Math.toDegrees(Math.atan2(y1-y2,x1-x2));
		//return Math.toDegrees(Math.atan2(x1-x2,y1-y2));
		//if(x2 > x1 && y2 > y1){
		double n = Math.toDegrees(Math.atan2(y2-y1,x2-x1));
		Log.i("Angulo1", ""+ n);
		
		return Math.toDegrees(Math.atan2(y2-y1,x2-x1));
		//}
		
		/*else if(x2 > x1 && y2 < y1){
			double n = Math.toDegrees(Math.atan2(y1-y2,x2-x1));
			Log.i("Angulo2", ""+ n);
			
			return Math.toDegrees(Math.atan2(y2-y1,x2-x1));
		}
		
		else if(x2 < x1 && y2 > y1){
			double n = Math.toDegrees(Math.atan2(y2-y1,x1-x2));
			Log.i("Angulo3", ""+ n);
			
			return Math.toDegrees(Math.atan2(y2-y1,x1-x2));
		}
		
		else if(x2 < x1 && y2 < y1){
			double n = Math.toDegrees(Math.atan2(y1-y2,x1-x2));
			Log.i("Angulo4", ""+ n);
			
			return Math.toDegrees(Math.atan2(y1-y2,x1-x2));
		}
		return 5;*/
		//return Math.toDegrees(Math.atan2(x2-x1,y1-y2));
		//return Math.toDegrees(Math.atan2(y1-y2,x2-x1));
	}
	//Dados Jogador
	public DadosDoCliente getDadosCliente(){
		return this.clientData;
	}
	public void setDadosCliente(DadosDoCliente dados){
		this.clientData = dados;
	}
	
	public String getNome() {
		return nome;
	}
	public void setNome(String nome) {
		this.nome = nome;
	}
	
	public Bitmap getImage(){
		return imagemPlayer;
	}
	public void setImage(Bitmap bitmap, int width, int height){
		this.imagemPlayer = bitmap;
		//this.imagemPlayer = CalculoImagem.getInstance().RedimensionarImagem(bitmap, height, width);
	}
	
	public Bitmap getTiro(){
		return this.tiro;
	}
	public void setTiro(Bitmap bitmap, int width, int height){
		this.tiro = bitmap;
		//this.tiro = CalculoImagem.getInstance().RedimensionarImagem(bitmap, height, width);
	}
	public boolean isAtirou(){
		return this.isAtirou;
	}
	public void Atirou(boolean atirou){
		this.isAtirou = atirou;
	}
	
	public String getTime(){
		return this.time;
	}
	public void setTime(String time){
		this.time = time;
		atualizarPaint();
	}

	public String getMinhaClasse(){
		return this.minhaClasse;
	}

	// Posicao
	public PointF getPosition() {
		return this.position;
	}
	public void setPosition(PointF pos) {
		this.position = pos;
	}
	// Dados para o jogo
	public int getMunicao(){
		return this.municao;
	}
	public void setMunicao(int municao){
		this.municao = municao;
	}
	public int getBalas(){
		return this.balas;
	}
	public void setBalas(int balas){
		this.balas = balas;
	}
	public int getVida(){
		return this.vida;
	}
	public void setVida(int vida){
		this.vida = vida;
	}
	public int getPontos(){
		return this.pontos;
	}
	public void setPontos(int pontos){
		this.pontos = pontos;
	}
	
	// Paint
	public Paint getPaint(){
		return this.paint;
	}
	private void atualizarPaint(){
		if(this.time == Const.TIMEVERMELHO)
		{
			this.paint.setColor(Color.RED);
		}else 
			if(this.time == Const.TIMEAZUL)
		{
			this.paint.setColor(Color.BLUE);
		}else{
			this.paint.setColor(Color.WHITE);
		}
	}
	
	// Dados para o servidor
	public String toString() {
		return "Jogador [nome=" + nome + ", x=" + this.position.x + ", y=" + this.position.y + "]";
	}
	public String toStringCSV() {
		return nome + "," + this.position.x + "," + this.position.y + ";";
	}
	
	// Destino Player
	public PointF getDestination(){
		return this.destination;
	}
	public void setDestination(PointF destination){
		this.destination = destination;
	}
	// Destino Tiro
		public Point getDestinationTiro(){
			return this.destinationTiro;
		}
		public void setDestinationTiro(Point destination){
			this.destinationTiro = destination;
		}
	//ViewPort
	public int getScrollX(){
		return this.plusScrollX;
	}
	public int getScrollY(){
		return this.plusScrollY;
	}
	public void dirViewPort(Point pnt1, Point pnt2, Point tamCanvas, RelativeLayout container, float scale){
	}
	
	// Verificacoes
	public boolean verificarTelaCheia(){
		return this.telaCheia;
	}
	public void verificarZoom(float x, float y, int altura, int largura, float scaleChange){
	}

	// Envios servidor
	public void sendDadosIniciais(){
		clientData.getCliente().Send(Protocolo.PROTOCOL_ID + "," + this.getNome() + ","+ this.getPosition().x + "," + this.getPosition().y 
				+ "," + this.getTime() + "," + this.getMinhaClasse());
	}
	public void sendTiro(Point toque){
	}
	public void sendPos(PointF pos){
	}
	public void sendNewTiro(Point toque){
		
	}
	
	// General
	public void setStartLocation(PointF point){
	}
	public void setEndLocation(PointF point){
	}
	public boolean getDrawTarget(){
		return this.drawTarget;
	}
	public void setDrawTarget(boolean draw){
		this.drawTarget = draw;
	}
	public boolean getRotateImage(){
		return this.rotateImage;
	}
	public void setRotateImage(boolean rotateImage){
		this.rotateImage = rotateImage;
	}
	
	public boolean getRotateImageTiro(){
		return this.rotateImageTiro;
	}
	public void setRotateImageTiro(boolean rotateImageTiro){
		this.rotateImageTiro = rotateImageTiro;
	}
}